/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月29日         luwei
 */

package com.coocaa.launcher.sidebar.setting.item.defaultlauncher;

import android.content.Context;
import android.view.KeyEvent;
import android.view.View;

import com.coocaa.launcher.framework.sidebar.item.Item;
import com.coocaa.launcher.framework.sidebar.item.ItemView;
import com.coocaa.launcher.settings.defaultsetting.DefaultLauncherSetting;
import com.coocaa.launcher.settings.defaultsetting.DefaultSettingBack;
import com.coocaa.launcher.sidebar.SettingData;
import com.tianci.appstore.ui.widget.TipToast;
import com.tianci.appstore.utils.SkyConstants;

public class DefaultLauncher extends Item implements DefaultSettingBack {
    private boolean mOn = false;
    private SettingData mSettingData = null;

    public DefaultLauncher(Context context) {
        super(context);
        DefaultLauncherSetting.getInstance(mContext).setDefaultSettingBack(this);
        mOn = DefaultLauncherSetting.getInstance(mContext).getDefaultLauncherStatus();
    }

    @Override
    public void onItemFocusChange(View v, boolean focus) {
        // TODO Auto-generated method stub
        super.onItemFocusChange(v, focus);
    }

    @Override
    public boolean onItemKeyEvent(View v, int keycode, KeyEvent keyEvent) {
        return false;
    }

    @Override
    public void onItemClick(View v) {
        if (mOn) {
            DefaultLauncherSetting.getInstance(mContext).clearDefaultSetting(mContext.getPackageName());
        } else {
            DefaultLauncherSetting.getInstance(mContext).setDefaultLauncher();
        }
        super.onItemClick(v);
    }

    @Override
    public ItemView createItemView() {
        // TODO Auto-generated method stub
        return new DefaultLauncherItem(mContext);
    }

    @Override
    public void onClearDefaultSetting() {
        mOn = false;
        ((DefaultLauncherItem) mItemView).setToggleButtonCheck(mOn);
    }

    @Override
    public void onClearDefaultSettingFailed() {
        mOn = true;
        ((DefaultLauncherItem) mItemView).setToggleButtonCheck(mOn);
    }

    @Override
    public void onBackStatus(boolean on) {
        mOn = on;
        ((DefaultLauncherItem) mItemView).setToggleButtonCheck(mOn);
    }

    @Override
    public void onOpenAppDetailFailed() {
        mOn = false;
        ((DefaultLauncherItem) mItemView).setToggleButtonCheck(mOn);
        TipToast.getInstance(mContext).showTipToast("设置默认桌面失败啦");
    }

    @Override
    public void setData(Object object) {
        this.mSettingData = (SettingData) object;
        ((DefaultLauncherItem) mItemView).updateIcon(SkyConstants.getIconId(mSettingData.iconId));
        ((DefaultLauncherItem) mItemView).updateName(SkyConstants.getStringId(mSettingData.titleId));
    }
}
